/*
 * IQMenu.java
 *
 * Created on 02 January 2009, 09:30
 */

package bluetoothserver.graphical;

import bluetoothserver.BluetoothInterface;
import bluetoothserver.dataStore.AnswerCollection;
import bluetoothserver.dataStore.Question;
import java.awt.Image;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author  Philip
 */
public class IQMenu extends javax.swing.JFrame {
    private ArrayList<Question> questions;
    private Iterator<Question> i;
    MainGUI gui;
    private int questionNo;
    private Question current;
    private Question next;
    private BluetoothInterface blue;
    private String[] rearranged;

    /** Creates new form IQMenu */
    public IQMenu( ArrayList<Question> qs,MainGUI g,BluetoothInterface b) {
        initComponents();
        gui = g;
        blue = b;
        questions = qs;
        questionNo = 1;
        this.setTitle("In Quiz Menu");
        this.setLocation(200, 200);
        i = questions.iterator();
        current = i.next();
        next = i.next();
        titleField.setText(current.getQuestion());
        questionNumber.setText(questionNo+" of "+questions.size());
        rearranged = blue.getRearranged();
        blue.SendQuestion(next);
        this.setIconImage(getIcon());
        this.setVisible(true);
        AnswerCollection a = AnswerCollection.getInstance();
        a.setMenu(this);
    }

    private Image getIcon(){
        Image im = null;
        try{
        File f = new File("Logo-small.png");
        im = javax.imageio.ImageIO.read(f);
        }catch(Exception e){}
        return im;
    }

    public Question getCurrentQuestion(){
        return current;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        titleField = new javax.swing.JTextField();
        responceField = new javax.swing.JTextField();
        back = new javax.swing.JButton();
        nextQ = new javax.swing.JButton();
        displayA = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        questionNumber = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        titleField.setEditable(false);

        responceField.setEditable(false);
        responceField.setText("0");

        back.setText("Back to Menu");
        back.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                backMousePressed(evt);
            }
        });

        nextQ.setText("Next Question");
        nextQ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                nextQMousePressed(evt);
            }
        });

        displayA.setText("Display Answers");
        displayA.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                displayAMousePressed(evt);
            }
        });

        jLabel1.setText("Question Title");

        jLabel2.setText("Number of Responces");

        jLabel3.setText("Question Number");

        questionNumber.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(titleField, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(responceField)
                            .addComponent(back, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(questionNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(81, 81, 81))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(displayA)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(nextQ)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(responceField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(questionNumber, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(back)
                    .addComponent(nextQ)
                    .addComponent(displayA))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void displayAMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_displayAMousePressed
    ResultsDisplay chart = new ResultsDisplay(current,rearranged);
    chart.run();
}//GEN-LAST:event_displayAMousePressed

private void nextQMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextQMousePressed
    questionNo++;

    if(nextQ.isEnabled()){
        questionNumber.setText(questionNo+" of "+questions.size());
        current = next;
        if(i.hasNext()){
            next = i.next();
        }
        rearranged = blue.getRearranged();
        blue.SendQuestion(next);
        titleField.setText(current.getQuestion());
        System.out.println(next.getQuestion());
        AnswerCollection answers = AnswerCollection.getInstance();
        answers.clearAnswers();
        this.clearCounter();
    }
    if(questionNo == questions.size()){
        nextQ.setEnabled(false);
    }
}//GEN-LAST:event_nextQMousePressed

private void backMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_backMousePressed
    this.setVisible(false);
    gui.setVisible(true);
    AnswerCollection a = AnswerCollection.getInstance();
    a.clearAnswers();
    this.clearCounter();  
}//GEN-LAST:event_backMousePressed

public void updateCounter(){
    String s = responceField.getText();
    int j = Integer.parseInt(s);
    j++;
    responceField.setText(j+"");
}

private void clearCounter(){
    responceField.setText(0+"");
}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton back;
    private javax.swing.JButton displayA;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton nextQ;
    private javax.swing.JTextField questionNumber;
    private javax.swing.JTextField responceField;
    private javax.swing.JTextField titleField;
    // End of variables declaration//GEN-END:variables

}
